package com.cqjtu.tto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqjtu.tto.dao.mapper.DepartmentMapper;
import com.cqjtu.tto.dao.mapper.RoleMapper;
import com.cqjtu.tto.dao.mapper.SysUserMapper;
import com.cqjtu.tto.dao.mapper.SysUserRoleMapper;
import com.cqjtu.tto.dao.pojo.Department;
import com.cqjtu.tto.dao.pojo.Role;
import com.cqjtu.tto.dao.pojo.SysUser;
import com.cqjtu.tto.dao.pojo.SysUserRole;
import com.cqjtu.tto.service.RegisterService;
import com.cqjtu.tto.utils.UserPasswordUtils;
import com.cqjtu.tto.vo.ErrorCode;
import com.cqjtu.tto.vo.Results;
import com.cqjtu.tto.vo.params.RegisterParam;
import io.swagger.annotations.ApiModel;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

import static com.cqjtu.tto.vo.ErrorCode.ACCOUNT_EXIST;

/**
 * @author：monkey-L
 * @description:
 * @date: 12:12 2021/12/11
 */
@Service
@ApiModel(description = "注册业务实现类",value = "注册业务实现类")
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /*
    * 实现逻辑:
    *   1.根据前端传回来的用户名在数据库中查询；
    *   2.如果存在用户，则返回用户名已经存在给前端；
    *   3.如果不存在用户，则查询用户传过来的部门名称（如果有的话）;
    *   4.如果存在该部门，则将用户的id和部门id设置好，然后写入用户数据，返回注册成功；
    *   5.如果不存在该部门，则返回该部门不存在；
    * @author monkey-L
    * @param RegisterParam registerParam
    * @return Results
    */
    @Override
    @Transactional
    public Results register(RegisterParam registerParam) {
        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        sysUserWrapper.eq(SysUser::getUserName,registerParam.getUserName()).last("limit 1");
        SysUser sysUser2 = sysUserMapper.selectOne(sysUserWrapper);
        if (sysUser2 != null) {
            return Results.fail(ErrorCode.ACCOUNT_EXIST.getCode(),ErrorCode.ACCOUNT_EXIST.getMsg());
        }
        SysUser sysUser = new SysUser();
        sysUser.setUserId(System.currentTimeMillis());
        BeanUtils.copyProperties(registerParam,sysUser);
//        md5加密密码
        String password = DigestUtils.md5Hex(registerParam.getUserPassword() + UserPasswordUtils.slat);
        sysUser.setUserPassword(password);
        LambdaQueryWrapper<Department> departmentWrapper = new LambdaQueryWrapper<>();
        departmentWrapper.eq(Department::getDepartmentName,registerParam.getDepartmentName())
                .last("limit 1");
        Department department = departmentMapper.selectOne(departmentWrapper);
        sysUser.setDepartmentId(department.getDepartmentId());
        sysUser.setUserDeleted(0);
        sysUserMapper.insert(sysUser);
        LambdaQueryWrapper<Role> roleWrapper = new LambdaQueryWrapper<>();
        roleWrapper.eq(Role::getRoleName,registerParam.getRoleName()).last("limit 1");
        Role role = roleMapper.selectOne(roleWrapper);
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(sysUser.getUserId());
        sysUserRole.setRoleId(role.getRoleId());
        sysUserRole.setUserRoleDeleted(0);
        sysUserRole.setUserRoleId(System.currentTimeMillis());
        sysUserRoleMapper.insert(sysUserRole);
        return Results.success("注册成功！！");
    }
}
